﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Ken_WPF_UI.View
{
    /// <summary>
    /// MediaElementWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MediaElementWindow : Window
    {
        private Timer timer;

        public MediaElementWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            timer = new Timer();
            timer.Interval = 1000;
            timer.Elapsed += Timer_Elapsed;
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                posSlider.Value = me.Position.TotalSeconds;
                Console.WriteLine("进度值:" + posSlider.Value);
                SetTime();
            }));
        }

        private void SetTime()
        {
            lbTime.Content = string.Format("{0:00}:{1:00}:{2:00}"
                , me.Position.Hours,me.Position.Minutes,me.Position.Seconds);
        }

        private void openBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = @"视频文件(*avi格式)|*.avi|视频文件(*mp4格式)|*.mp4|视频文件(*wmv格式)|*.wmv";

            if (openFileDialog.ShowDialog()!=false)
            {
                string fileName = openFileDialog.FileName;
                me.Source = new Uri(fileName, UriKind.Absolute);
                playBtn.IsEnabled = true;
                me.Play();
                timer.Start();
                playBtn.Content = "暂停";
            }

        }

        /// <summary>
        /// 播放或暂停
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void playBtn_Click(object sender, RoutedEventArgs e)
        {
            SetPlayer(true);
            PlayOrPause();
        }


        /// <summary>
        /// 设置下面控件的可用性
        /// </summary>
        /// <param name="status"></param>
        private void SetPlayer(bool status)
        {
            this.openBtn.IsEnabled = status;
            this.playBtn.IsEnabled = status;
            this.stopBtn.IsEnabled = status;
            this.backBtn.IsEnabled = status;
            this.forwardBtn.IsEnabled = status;
        }

        /// <summary>
        /// 播放或暂停
        /// </summary>
        private void PlayOrPause()
        {
            if (playBtn.Content.ToString() == "暂停")
            {
                me.Pause();
                timer.Stop();
                playBtn.Content = "播放";
                me.ToolTip = "单击播放";
            }
            else
            {
                me.Play();
                timer.Start();
                playBtn.Content = "暂停";
                me.ToolTip = "单击暂停";
                SetTime();
            }
        }


        /// <summary>
        /// 停止播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void stopBtn_Click(object sender, RoutedEventArgs e)
        {
            me.Stop();
            posSlider.Value = 0;
            lbTime.Content = "0:0:0";
            playBtn.Content = "播放";
            timer.Stop();
        }


        /// <summary>
        /// 快进
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void forwardBtn_Click(object sender, RoutedEventArgs e)
        {
            me.Pause();
            timer.Stop();
            me.Position = me.Position + TimeSpan.FromSeconds(10);
            SetTime();
            me.Play();
            timer.Start();
        }

        /// <summary>
        /// 快退
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backBtn_Click(object sender, RoutedEventArgs e)
        {
            me.Pause();
            timer.Stop();
            me.Position = me.Position - TimeSpan.FromSeconds(10);
            SetTime();
            me.Play();
            timer.Start();
        }

        /// <summary>
        /// 点击播放器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void me_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PlayOrPause();
        }

        /// <summary>
        /// ，媒体资源加载完毕后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void me_MediaOpened(object sender, RoutedEventArgs e)
        {
            posSlider.Maximum = me.NaturalDuration.TimeSpan.TotalSeconds;
            lbTime.Content = "0:0:0";
            SetPlayer(true);
        }

        /// <summary>
        /// 滑块鼠标左键拖动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void posSlider_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            me.Pause();
            timer.Stop();
            int val = (int)posSlider.Value;
            me.Position = new TimeSpan(0, 0, 0, val);
            me.Play();
            timer.Start();
        }

        /// <summary>
        /// 播放结束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void me_MediaEnded(object sender, RoutedEventArgs e)
        {
            me.Stop();
            timer.Stop();
        }
    }
}
